package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.MessageDAO;
import entity.Message;
import tools.BaseDAO;

public class MessageDaoImpl extends BaseDAO implements MessageDAO{

	@Override
	public List<Message> getMessageByReceiveUname(String uname) {
		List<Message> messages = new ArrayList<Message>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select * from tbl_message where receiveUname=?";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, uname);
			rs = ps.executeQuery();
			while (rs.next()) {
				Message m = new Message();
				getModel(m, rs);
				messages.add(m);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
		return messages;
	}
	
	@Override
	public Message getMessageByMid(int mid) {
		Message message = new Message();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "select * from tbl_message where id=?";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, mid);
			rs = ps.executeQuery();
			if (rs.next()) {
				getModel(message, rs);
				
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
		return message;
	}

	private void getModel(Message m, ResultSet rs) throws Exception{
		m.setId(rs.getInt(1));
		m.setNote(rs.getString(2));
		m.setSendUname(rs.getString(3));
		m.setReceiveUname(rs.getString(4));
		m.setPostTime(rs.getString(5));
		m.setReadSign(rs.getInt(6));
	}

	@Override
	public int deleteMessageById(String id) {
		int result = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "delete from tbl_message where id in ("+id+")";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			result = ps.executeUpdate();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
		return result;
	}

	@Override
	public void sendMessage(Message m) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_message values(null,?,?,?,?,?)";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, m.getNote());
			ps.setString(2, m.getSendUname());
			ps.setString(3, m.getReceiveUname());
			ps.setString(4, m.getPostTime());
			ps.setInt(5, m.getReadSign());
			ps.executeUpdate();
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
	}

	@Override
	public boolean updateMessage(int mid) {
		boolean flag = false;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "update tbl_message set readSign=1 where id=?";
		
		try {
			conn = getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, mid);
			flag = ps.executeUpdate()>0 ? true :false;
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			closeAll(rs, ps, conn);
		}
		return flag;
	}

}
